import Taro from '@tarojs/taro';


const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}

export function formatTime (date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return (
    [year, month, day].map(formatNumber).join('/') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  )
}

export function showToast(title, icon, duration=3000) {
    return new Promise((resolve) => {
        Taro.showToast({
            title: title,
            icon: icon ? icon : 'none',
            duration: duration
        }).then(() => {
           setTimeout(() => {
               resolve(true);
           }, duration)
        })
    })
}

export function showTipModal(content, title='提示') {
    Taro.showModal({
        title,
        content,
        showCancel: false,
    })
}

export function imgZoom(width, height) {
    //---------利用canvas压缩图片--------------
    console.log(width);
    console.log(height);
    let canvasWidth = width; //图片原始长宽
    let canvasHeight = height;
    let ratio = 1;
    if (canvasHeight > canvasWidth) {
        ratio = canvasHeight / 1024;
        canvasHeight = 1024;
        canvasWidth = parseInt(canvasWidth / ratio);
    } else {
        ratio = canvasWidth / 1280;
        canvasWidth = 1280;
        canvasHeight = parseInt(canvasHeight / ratio);
    }
    return {canvasWidth, canvasHeight}
}
